package com.ocom.webapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.MapKey;
import com.ocom.common.entity.master.EntityCompanyList;
import com.ocom.common.entity.master.EntityMngUserInfo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.webapi.controller.VisitorRecordContoller;
import com.ocom.webapi.feign.CompanyClientService;
import com.ocom.webapi.feign.HumanClientService;
import com.ocom.webapi.service.UnitAccountService;
import com.ocom.webapi.vo.unitaccount.*;
import lombok.extern.log4j.Log4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j
@Service
public class UnitAccountServiceImpl implements UnitAccountService {
    private final Logger log = LoggerFactory.getLogger(UnitAccountServiceImpl.class);
    @Autowired
    private CompanyClientService companyClientService;

    @Autowired
    private HumanClientService humanClientService;
    /**
     * 单位账号列表（UnitAccount/onShow）
     * */
    @Override
    public Result UnitAccountOnShow(OnShowVo onShowVo) {
        JSONObject jsonObject = new JSONObject();
        MapKey mapKey = new MapKey();

        jsonObject.put(mapKey.getKey_key(),onShowVo.getComId());
        jsonObject.put(mapKey.getKey_userName(),onShowVo.getAccount());
        jsonObject.put(mapKey.getKey_status(),onShowVo.getStatus());
        jsonObject.put(mapKey.getKey_role(),onShowVo.getAccountType());
        jsonObject.put(mapKey.getKey_page(),onShowVo.getPage());
        jsonObject.put(mapKey.getKey_size(),onShowVo.getSize());
        Result mngUserInfo = humanClientService.getMngUserInfo(jsonObject);
        if (mngUserInfo.getCode()!=200){
            return ResultUtil.error(mngUserInfo.getCode());
        }
        JSONObject retJsonObject=JSON.parseObject(JSON.toJSONString(mngUserInfo.getData()));
        List<EntityMngUserInfo> entityMngUserInfoList= JSON.parseArray(JSON.toJSONString(retJsonObject.get("list")), EntityMngUserInfo.class);
        List<Map<String,Object>> list=new ArrayList<>();

        for (EntityMngUserInfo entityMngUserInfo: entityMngUserInfoList) {
            Map<String, Object> map = new HashMap<>();
            map.put(mapKey.getKey_id(),entityMngUserInfo.getId());
            map.put(mapKey.getKey_comId(),entityMngUserInfo.getComId());
            map.put(mapKey.getKey_account(),entityMngUserInfo.getUserName());
            map.put(mapKey.getKey_accountType(),entityMngUserInfo.getRole());
            map.put(mapKey.getKey_status(),entityMngUserInfo.getStatus());
            map.put(mapKey.getKey_createTime(),entityMngUserInfo.getCreateTime());
            map.put(mapKey.getKey_updateTime(),entityMngUserInfo.getUpdateTime());
            Long companyId = entityMngUserInfo.getComId();
            Result result = companyClientService.GetCompanyListInfoOne(companyId);
            if (result.getCode()==200){
                EntityCompanyList entityCompanyList = JSON.parseObject(JSON.toJSONString(result.getData()), EntityCompanyList.class);
                assert entityCompanyList.getComName() != null;
                map.put(mapKey.getKey_comName(),entityCompanyList.getComName());
            }

            list.add(map);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total",JSON.toJSONString(retJsonObject.get("total")));
        map.put("list",list);
        log.info("单位账户列表信息:【{}】",map);
        return ResultUtil.success(map);
    }

    /**
     * 新增单位账号（UnitAccount/add）
     * */
    @Override
    public Result UnitAccountAdd(AddVo addVo) {
        JSONObject jsonObject = new JSONObject();
        MapKey mapKey = new MapKey();
        jsonObject.put(mapKey.getKey_userName(),addVo.getAccount());
        jsonObject.put(mapKey.getKey_password(),addVo.getPassword());
        jsonObject.put(mapKey.getKey_role(),addVo.getAccountType());
        jsonObject.put(mapKey.getKey_comId(),addVo.getComId());
        jsonObject.put(mapKey.getKey_status(),0);
        jsonObject.put(mapKey.getKey_phone(),addVo.getPhone());
        return humanClientService.addMngUserInfo(jsonObject);
    }

    /**
     * 修改单位账号（UnitAccount/modify）
     * */
    @Override
    public Result UnitAccountModify(ModifyVo modifyVo) {
        JSONObject jsonObject = new JSONObject();
        MapKey mapKey = new MapKey();
        jsonObject.put(mapKey.getKey_userName(),modifyVo.getAccount());
        jsonObject.put(mapKey.getKey_role(),modifyVo.getAccountType());
        jsonObject.put(mapKey.getKey_comId(),modifyVo.getComId());
        jsonObject.put(mapKey.getKey_id(),modifyVo.getId());
        jsonObject.put(mapKey.getKey_phone(),modifyVo.getPhone());
        return humanClientService.UpdateMngUserInfo(jsonObject);
    }
    /**
     * 启用/禁用单位账号（UnitAccount/modifyStatus）
     * */
    @Override
    public Result UnitAccountModifyStatus(ModifyStatusVo modifyStatusVo) {
        JSONObject jsonObject = new JSONObject();
        MapKey mapKey = new MapKey();
        jsonObject.put(mapKey.getKey_userName(),modifyStatusVo.getAccount());
        jsonObject.put(mapKey.getKey_status(),modifyStatusVo.getStatus());
        jsonObject.put(mapKey.getKey_comId(),modifyStatusVo.getComId());
        jsonObject.put(mapKey.getKey_id(),modifyStatusVo.getId());
        return humanClientService.UpdateMngUserInfo(jsonObject);
    }

    /**
     * 修改单位账号密码（UnitAccout/modifyPwd）
     * */
    @Override
    public Result UnitAccountModifyPwd(ModifyPwdVo modifyStatusVo) {
        JSONObject jsonObject = new JSONObject();
        MapKey mapKey = new MapKey();
        jsonObject.put(mapKey.getKey_userName(),modifyStatusVo.getAccount());
        jsonObject.put(mapKey.getKey_password(),modifyStatusVo.getPassword());
        jsonObject.put(mapKey.getKey_comId(),modifyStatusVo.getComId());
        jsonObject.put(mapKey.getKey_id(),modifyStatusVo.getId());
        return humanClientService.UpdateMngUserInfo(jsonObject);
    }
}
